<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once 'OaLogic.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppConfigOrderModel.php';

class OaAppOrderLogic extends OaLogic
{
    
    /**
     * 将测试环境成功完成的任务复制到正式环境，任务状态为无效
     * @param string $orderId
     * @return boolean
     */
    public static function copyTestTaskToProduction($orderId)
    {
        $logger = self::getLogger();
    	$appConfigOrderModel = new OaAppConfigOrderModel($logger);
        $tasks = $appConfigOrderModel->getConfigOrderTasks($orderId, 
            OaAppConfigOrderModel::CONFIG_TASK_ENV_TEST);
        if ($tasks === false) {
            $logger->err("get tasks failed, order-$orderId");
            return false;
        }
        foreach ($tasks as $task) {
            if (in_array($task['job_name'], array(
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_BILLING_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_BILLING_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_BONUS_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_BONUS_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_HELP_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_HELP_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_IBW_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_IBW_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_IGW_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_IGW_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_LOGIN_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_LOGIN_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_PAY_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_PAY_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_POINTS_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_POINTS_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_OPEN_PREVENT_OBSESSION_SERVICE, 
                OaAppConfigOrderModel::CONFIG_JOB_MODIFY_PREVENT_OBSESSION_SERVICE
            ))) {
                if (!$appConfigOrderModel->deleteConfigTaskByJobName($orderId, $task['job_name'], 
                    OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION)) {
                    $logger->err("delete task of production failed");
                    return false;
                }
                if (!$appConfigOrderModel->createConfigTask($orderId, $task['job_name'], $task['data'], 
                        OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION, array(
                			'status' => OaAppConfigOrderModel::CONFIG_TASK_STATUS_INVALID
                        )
                    )
                ) {
                    $logger->err("create task failed, order-$orderId");
                    return false;
                }
            }
        }
        return true;
    }
    
    /**
     * 保存订单服务配置到应用
     * @param string $orderId
     * @param int $env
     * @return boolean
     */
    public static function saveOrderServiceConfigToApp($orderId, 
        $env = OaAppConfigOrderModel::CONFIG_TASK_ENV_TEST)
    {
        $logger = self::getLogger();
    	$appConfigOrderModel = new OaAppConfigOrderModel($logger);
        $order = $appConfigOrderModel->getConfigOrder($orderId);
        if (!is_array($order)) {
            $logger->err("order not exists");
            return false;
        }
        $tasks = $appConfigOrderModel->getConfigOrderTasks($orderId, $env);
        if ($tasks === false) {
            $logger->err("get config order tasks failed");
            return false;
        }
        $appModel = new OaAppModel($env, $logger);
        foreach ($tasks as $task) {
            if ($task['job_name'] == 'open_login_service') {
                $row = array(
                    'service_login_status' => $task['status'],
                    'service_login' => $task['data']
                );
            } else if ($task['job_name'] == 'open_ibw_service') {
                $row = array(
                    'service_ibw_status' => $task['status'],
                    'service_ibw' => $task['data']
                );
            } else if ($task['job_name'] == 'open_igw_service') {
                $row = array(
                    'service_igw_status' => $task['status'],
                    'service_igw' => $task['data']
                );
            } else if ($task['job_name'] == 'open_pay_service') {
                $row = array(
                    'service_pay_status' => $task['status'],
                    'service_pay' => $task['data']
                );
            } else if ($task['job_name'] == 'open_billing_service') {
                $row = array(
                    'service_billing_status' => $task['status'],
                    'service_billing' => $task['data']
                );
            } else if ($task['job_name'] == 'open_help_service') {
                $row = array(
                    'service_help_status' => $task['status'],
                    'service_help' => $task['data']
                );
            } else if ($task['job_name'] == 'open_bonus_service') {
                $row = array(
                    'service_bonus_status' => $task['status'],
                    'service_bonus' => $task['data']
                );
            } else if ($task['job_name'] == 'open_points_service') {
                $row = array(
                    'service_points_status' => $task['status'],
                    'service_points' => $task['data']
                );
            } else if ($task['job_name'] == 'open_prevent_obsession_service') {
                $row = array(
                    'service_prevent_obsession_status' => $task['status'],
                    'service_prevent_obsession' => $task['data']
                );
            } else {
                continue;
            }
            if (!$appModel->modifyApp($order['app_id'], $row)) {
                $logger->err("save task config failed");
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * 从应用读取服务配置到订单
     * @param string $orderId
     * @param int $env
     * @return boolean
     */
    public static function readOrderServiceConfigFromApp($orderId, 
        $env = OaAppConfigOrderModel::CONFIG_TASK_ENV_TEST)
    {
        $logger = self::getLogger();
    	$appConfigOrderModel = new OaAppConfigOrderModel($logger);
        $order = $appConfigOrderModel->getConfigOrder($orderId);
        if (!is_array($order)) {
            $logger->err("order not exists");
            return false;
        }
        $appModel = new OaAppModel($env, $logger);
        $app = $appModel->getApp($order['app_id']);
        if (!is_array($app)) {
            $logger->err("app not exists");
            return false;
        }
        
        if ($app['service_login_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_LOGIN_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_LOGIN_SERVICE, 
                $app['service_login'], $env, array(
                	'status' => $app['service_login_status']
                )
            )) {
                $logger->err("init login service failed");
                return false;
            }
        }
        if ($app['service_ibw_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_IBW_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_IBW_SERVICE, 
                $app['service_ibw'], $env, array(
                	'status' => $app['service_ibw_status']
                )
            )) {
                $logger->err("init ibw service failed");
                return false;
            }
        }
        if ($app['service_igw_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_IGW_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_IGW_SERVICE, 
                $app['service_igw'], $env, array(
                	'status' => $app['service_igw_status']
                )
            )) {
                $logger->err("init igw service failed");
                return false;
            }
        }
        if ($app['service_pay_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_PAY_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_PAY_SERVICE, 
                $app['service_pay'], $env, array(
                	'status' => $app['service_pay_status']
                )
            )) {
                $logger->err("init pay service failed");
                return false;
            }
        }
        if ($app['service_billing_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_BILLING_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_BILLING_SERVICE, 
                $app['service_billing'], $env, array(
                	'status' => $app['service_billing_status']
                )
            )) {
                $logger->err("init billing service failed");
                return false;
            }
        }
        if ($app['service_help_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_HELP_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_HELP_SERVICE, 
                $app['service_help'], $env, array(
                	'status' => $app['service_help_status']
                )
            )) {
                $logger->err("init help service failed");
                return false;
            }
        }
        if ($app['service_bonus_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_BONUS_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_BONUS_SERVICE, 
                $app['service_bonus'], $env, array(
                	'status' => $app['service_bonus_status']
                )
            )) {
                $logger->err("init bonus service failed");
                return false;
            }
        }
        if ($app['service_points_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_POINTS_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_POINTS_SERVICE, 
                $app['service_points'], $env, array(
                	'status' => $app['service_points_status']
                )
            )) {
                $logger->err("init points service failed");
                return false;
            }
        }
        if ($app['service_prevent_obsession_status'] > 0) {
        	$appConfigOrderModel->deleteConfigTaskByJobName($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_PREVENT_OBSESSION_SERVICE, OaAppConfigOrderModel::CONFIG_TASK_ENV_PRODUCTION);
            if (!$appConfigOrderModel->createConfigTask($orderId, OaAppConfigOrderModel::CONFIG_JOB_OPEN_PREVENT_OBSESSION_SERVICE, 
                $app['service_prevent_obsession'], $env, array(
                	'status' => $app['service_prevent_obsession_status']
                )
            )) {
                $logger->err("init prevent obsession service failed");
                return false;
            }
        }
        
        return true;
    }
}
